<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index()	{
		if ($this->session->userdata('user')) {
			redirect($this->session->userdata('user')->site_role);
		}

		$this->load->view('master');
	}

	public function login() {	
		if ($this->form_validation->run() == FALSE)
		{
			redirect('/');
			return FALSE;
		}

		$this->load->model('user_model', '', TRUE);

		if ($this->user_model->login($this->input->post())) {
			if ($this->session->userdata('user')->wizard_status == 'active')
			{
				redirect('new_user');
			}
			redirect($this->session->userdata('user')->site_role);
		}

		$this->session->set_flashdata('error', 'Invalid Login!');
		redirect('/');
	}

	public function logout() {
		$this->session->unset_userdata('user');
		redirect('/');
	}

	public function register() {
		// Test if the register form was submitted, if not then display it
		if (!$this->input->post())
		{
			$this->load->view('master');
			return TRUE;
		}

		$this->load->model('user_model', '', TRUE);
	
		// Form validation has failed, redisplay the form including any error messages
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('master');
			return FALSE;
		}

		// Perform the registration logic and store the new user information in an array
		if ($user_array = $this->user_model->register($this->input->post()))
		{
			// Returns FALSE on success, otherwise we want to capture and display the error message
			if ($error = $this->_mail($user_array))
			{
				// Failed to send email, reload form and display errors.  NOTE: registration was successful in the DB
				$this->session->set_flashdata('register_error', 'Failed to send email');
				redirect('register');
			}

			$this->session->set_flashdata('success', '<div class="success">Registration was successful!.  Please check you email for the activation link</div>');
			redirect('/');
		}

		// Registration failed so reload the form and display any error messages
		redirect('register');
	}
    
    /**
	 * 
	 * This link will come from the initial registration email and will contain the user's
	 * access code.  The user will be asked to login and if successful, the access code
	 * will be validated completing the registration process
	 * 
	 * Example:  http://localhost/home/validate/sdfojwr0wjfowjfcv
	 * 
	 */
	public function validate()
	{
		$this->data['access_code'] = $this->uri->segment(3);
		
		$this->load->model('user_model', '', TRUE);

		if ($user = $this->user_model->check_access_code($this->data['access_code']))
		{
			$this->session->set_userdata('user', $user);
			redirect('new_user');
		}

		redirect('/home/validate_error/' . $this->data['access_code']);
	}

	public function validate_error()
	{
		$access_code = $this->uri->segment(3);		
		$this->load->model('user_model', '', TRUE);

		if ($user_array = $this->user_model->get_user($access_code))
		{
			if ($this->input->post())
			{
				if ($error = $this->_mail($user_array))
				{
					$data['error'] = '<div class="error">Failed to send email</div>';
					$this->load->view('master', $data);
					return FALSE;
				}

				$data['error'] = '<div class="success">Email has been sent to ' . $user_array['email'] . '</div>';
				$this->load->view('master', $data);
				return FALSE;
			}
		}

		$data['error'] = '<div class="error">Invalid access code.</div>';
		$this->load->view('master', $data);
		return FALSE;
	}

	public function remove()
	{
		$access_code = $this->uri->segment(3);		
		$this->load->model('user_model', '', TRUE);
		$this->user_model->remove_new_user($access_code);
		$this->session->set_flashdata('error', '<div class="success">Email successfully removed!</div>');
		redirect('/');
	}

	public function about()
	{
		$this->load->view('master');
	}

	public function contact()
	{
		// Test if the contact form was submitted, if not then display it
		if (!$this->input->post())
		{
			$this->load->view('master');
			return TRUE;
		}
	
		// Form validation has failed, redisplay the form including any error messages
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('master');
			return FALSE;
		}

		$this->load->library('email');
        $this->email->set_newline("\r\n");
        $this->email->from($this->input->post('email'), $this->input->post('name'));
        $this->email->to('therank.me@gmail.com');
        $this->email->bcc('mlynch1985@gmail.com');
        $this->email->subject('Message Received from ' . $this->input->post('name'));        
        $email = $this->input->post('message');
		$this->email->message($email);       

        if ($this->email->send())
        {
        	$this->session->set_flashdata('contact_error', '<div class="success">Your message has been sent!</div>');
        	redirect('home/contact');
        }

    	$this->session->set_flashdata('contact_error', '<div class="error">Failed to send email</div>');
    	redirect('home/contact');
	}

	public function policy()
	{
		$this->load->view('master');
	}

	public function terms()
	{
		$this->load->view('master');
	}

	public function _mail($user_array)
	{
        $this->load->library('email');
        $this->email->set_newline("\r\n");
        $this->email->from('therank.me@gmail.com', 'theRank.ME');
        $this->email->to($user_array['email']);
        $this->email->bcc('mlynch1985@gmail.com');
        $this->email->subject('Welcome to theRank.ME');        
        $email = $this->load->view('emails/email_authentication', $user_array, TRUE);
		$this->email->message($email);       

        if ($this->email->send())
        	return FALSE; // Successful

        return $this->email->print_debugger();  // Failed
	}
    
    
}